package com.etc.service.imp;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.etc.common.ServiceException;
import com.etc.mapper.StudentMapper;
import com.etc.pojo.Student;
import com.etc.service.IStudentService;

@Service
public class StudentServiceImp implements IStudentService {

    @Autowired
    StudentMapper mapper;
    
    @Override
    public List<Student> findByCondition(Student stu) {
        
        
        return mapper.findByCondition(stu);
    }

    @Transactional(rollbackFor=Exception.class)
    @Override
    public void deleteById(Integer id) throws ServiceException {
        
        int r = mapper.deleteById(id);
        if (r != 1) {
            throw new ServiceException("删除功能失败");
        }
    }

    @Override
    public Student findById(Integer id) {
        return mapper.findById(id);
    }

    @Transactional(rollbackFor=Exception.class)
    @Override
    public Student update(Student stu) throws ServiceException {
        Student stu1 = mapper.findByIdVersion(stu); // 乐观锁判定， 加悲观锁行级锁
        
        if (stu1 == null) {
            throw new ServiceException("这样不行呀，别人比你快");
        }
        
        int i = mapper.update(stu); 
        
        if (i != 1) {
            throw new ServiceException("这个有点问题更新失败了，联系管理员");
        }
        
        return mapper.findById(stu.getId());
    }

}
